package com.mes.eventhandler;

import com.mes.data.EmesProcess;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;

public class EmesProcessEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesProcess.ENTITY_NAME) };
	protected Logger logger = Logger.getLogger(this.getClass());

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	private void throwMessage(String message) {
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		ConnectionProvider conn = new DalConnectionProvider(false);
		throw new OBException(Utility.messageBD(conn, message, language));
	}

	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesProcess emesProcess = (EmesProcess) event.getTargetInstance();
		// 当前工艺路线ID
		String p_id = emesProcess.getEmesProcessRoute().getId();
		// 当前工序号
		long no = emesProcess.getRouteNo();
		String hql = "select er from " + EmesProcess.ENTITY_NAME + " er where er.routeNo = ? and er.emesProcessRoute.id = ?";
		Query query = OBDal.getInstance().getSession().createQuery(hql);
		query.setLong(0, no);
		query.setString(1, p_id);
		EmesProcess newProcess = (EmesProcess) query.uniqueResult();
		if (newProcess != null) {
			throwMessage("当前工序编码在此工艺路线中有重复，请仔细检查");
		} else {
			final Property nameProperty = entities[0].getProperty(EmesProcess.PROPERTY_NAME);
			event.setCurrentState(nameProperty, emesProcess.getEmesProcessOpr().getName());

			final Property codeProperty = entities[0].getProperty(EmesProcess.PROPERTY_CODE);
			event.setCurrentState(codeProperty, emesProcess.getEmesProcessOpr().getCode());
		}
	}
	
	public void onUpdate(@Observes EntityUpdateEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesProcess emesProcess = (EmesProcess) event.getTargetInstance();
		// 当前工艺路线ID
		String p_id = emesProcess.getEmesProcessRoute().getId();
		// 当前工序号
		long no = emesProcess.getRouteNo();
		String hql = "select er from " + EmesProcess.ENTITY_NAME + " er " +
				"where er.routeNo = ? and er.emesProcessRoute.id = ? and er.id <> ?";
		Query query = OBDal.getInstance().getSession().createQuery(hql);
		query.setLong(0, no);
		query.setString(1, p_id);
		query.setString(2, emesProcess.getId());
		EmesProcess newProcess = (EmesProcess) query.uniqueResult();
		if (newProcess != null) {
			throwMessage("当前工序编码在此工艺路线中有重复，请仔细检查");
		} else {
			final Property nameProperty = entities[0].getProperty(EmesProcess.PROPERTY_NAME);
			event.setCurrentState(nameProperty, emesProcess.getEmesProcessOpr().getName());

			final Property codeProperty = entities[0].getProperty(EmesProcess.PROPERTY_CODE);
			event.setCurrentState(codeProperty, emesProcess.getEmesProcessOpr().getCode());
		}
	}
}
